package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.FinanceReportBI;
import com.cleaningcloud.cld.domain.HumanResourcesBI;
import com.cleaningcloud.cld.domain.vo.CldBiDataVo;
import com.cleaningcloud.cld.mapper.FinanceReportBIMapper;
import com.cleaningcloud.cld.mapper.HumanResourcesBIMapper;
import com.cleaningcloud.cld.service.IFinanceReportBIService;
import com.cleaningcloud.cld.service.IHumanResourcesBIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @className 人资履约BI业务实现类
 * @description //TODO
 * @author Shaw
 * @date: 2025-03-06 16:33
 * @param: //TODO
 */
@Service
public class HumanResourcesServiceBIImpl implements IHumanResourcesBIService {

    //系统报表映射
    @Autowired
    private HumanResourcesBIMapper humanResourcesBIMapper;

    /**
     * @Author Shaw
     * @Description 项目岗位执勤占比图
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getProjectPostRate(){
        return humanResourcesBIMapper.getProjectPostRate();
    }

    /**
     * @Author Shaw
     * @Description 日工作计划完成占比图
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getSunWorkPlanSuccessRate(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        HumanResourcesBI humanResourcesBI = new HumanResourcesBI();
        String dateVal = "";
        if(month < 10){
            dateVal = year + "0" + month;
        }else{
            dateVal = year + "" + month;
        }
        humanResourcesBI.setDateVal(dateVal);
        return humanResourcesBIMapper.getSunWorkPlanSuccessRate(humanResourcesBI);
    }

    /**
     * @Author Shaw
     * @Description 专项工作计划完成占比图
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getSpecialWorkPlanSuccessRate(){
        return humanResourcesBIMapper.getSpecialWorkPlanSuccessRate();
    }

    /**
     * @Author Shaw
     * @Description 投诉处置完成占比图
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getComplaintsSuccessRate(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        HumanResourcesBI humanResourcesBI = new HumanResourcesBI();
        String dateVal = "";
        if(month < 10){
            dateVal = year + "0" + month;
        }else{
            dateVal = year + "" + month;
        }
        humanResourcesBI.setDateVal(dateVal);
        return humanResourcesBIMapper.getComplaintsSuccessRate(humanResourcesBI);
    }

    /**
     * @Author Shaw
     * @Description 培训计划完成占比图
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getTrainSuccessRate(){
        return humanResourcesBIMapper.getTrainSuccessRate();
    }

    /**
     * @Author Shaw
     * @Description 员工保险占比图
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String, Object> getInsuranceRate(){
        return humanResourcesBIMapper.getInsuranceRate();
    }

    /**
     * @Author Shaw
     * @Description 按当月查询实际薪酬(日走势图)
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public CldBiDataVo getMonthSalary() {
        CldBiDataVo cbv = new CldBiDataVo();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        BigDecimal[] basicSalary = new BigDecimal[days];
        BigDecimal[] performanceAllowance = new BigDecimal[days];
        BigDecimal[] overTimeSalary = new BigDecimal[days];
        BigDecimal[] benefitAllowance = new BigDecimal[days];
        BigDecimal[] rewardsMoney = new BigDecimal[days];
        BigDecimal[] insuranceMoney = new BigDecimal[days];
        BigDecimal[] clothesMoney = new BigDecimal[days];
        BigDecimal[] workPlanMoney = new BigDecimal[days];
        BigDecimal[] trainingCost = new BigDecimal[days];
        BigDecimal[] hireCost = new BigDecimal[days];
        BigDecimal[] pv = new BigDecimal[days];
        BigDecimal[] rv = new BigDecimal[days];
        HumanResourcesBI bi = new HumanResourcesBI();
        bi.setDateYear(year);
        bi.setDateMonth(month);
        for (int i=1 ; i<=days ; i++) {
            bi.setDateDay(i);
            HumanResourcesBI obj = humanResourcesBIMapper.getMonthSalary(bi);

            basicSalary[i-1] = obj.getBasicSalary();
            performanceAllowance[i-1] = obj.getPerformanceAllowance();
            overTimeSalary[i-1] = obj.getOverTimeSalary();
            benefitAllowance[i-1] = obj.getBenefitAllowance();
            rewardsMoney[i-1] = obj.getRewardsMoney();
            insuranceMoney[i-1] = obj.getInsuranceMoney();
            clothesMoney[i-1] = obj.getClothesMoney();
            workPlanMoney[i-1] = obj.getWorkPlanMoney();
            trainingCost[i-1] = obj.getTrainingCost();
            hireCost[i-1] = obj.getHireCost();
            pv[i-1] = obj.getPv();
            rv[i-1] = obj.getRv();
        }
        Object[] dataVal = new Object[12];
        dataVal[0] =  basicSalary;
        dataVal[1] =  performanceAllowance;
        dataVal[2] =  overTimeSalary;
        dataVal[3] =  benefitAllowance;
        dataVal[4] =  rewardsMoney;
        dataVal[5] =  insuranceMoney;
        dataVal[6] =  clothesMoney;
        dataVal[7] =  workPlanMoney;
        dataVal[8] =  trainingCost;
        dataVal[9] =  hireCost;
        dataVal[10] =  pv;
        dataVal[11] =  rv;
        cbv.setDataVal(dataVal);
        return cbv;
    }

    /**
     * @Author Shaw
     * @Description 按当月查询实际薪酬(日走势图)
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public HumanResourcesBI getMonthSalaryTable() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        HumanResourcesBI bi = new HumanResourcesBI();
        bi.setDateYear(year);
        bi.setDateMonth(month);
        return humanResourcesBIMapper.getMonthSalaryTable(bi);
    }

    /**
     * @Author Shaw
     * @Description 按项目查询薪酬明细
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<HumanResourcesBI> getMonthSalaryDetailsByProject() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        HumanResourcesBI bi = new HumanResourcesBI();
        bi.setDateYear(year);
        bi.setDateMonth(month);
        return humanResourcesBIMapper.getMonthSalaryDetailsByProject(bi);
    }

    /**
     * @Author Shaw
     * @Description 按性别年龄查询人员在岗
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String, Object>  getStaffBySexAge(){
        return humanResourcesBIMapper.getStaffBySexAge();
    }

    /**
     * @Author Shaw
     * @Description 按管理类型查询管理者占比
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String, Object>  getStaffManagerRate(){
        return humanResourcesBIMapper.getStaffManagerRate();
    }

    /**
     * @Author Shaw
     * @Description 按区域查询管理者职别
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<HumanResourcesBI> getStaffManagerTable(){
        return humanResourcesBIMapper.getStaffManagerTable();
    }

    /**
     * @Author Shaw
     * @Description 按项目查询项目员工状态人数
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<HumanResourcesBI> getStaffStatusByProject(){
        return humanResourcesBIMapper.getStaffStatusByProject();
    }

    /**
     * @Author Shaw
     * @Description 按当月查询日计划完成情况(日走势图)
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public CldBiDataVo getMonthWorkPlan() {
        CldBiDataVo cbv = new CldBiDataVo();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        Integer[] sumNumber = new Integer[days];
        Integer[] successNumber = new Integer[days];
        Integer[] unSuccessNumber = new Integer[days];
        HumanResourcesBI bi = new HumanResourcesBI();
        for (int i=1 ; i<=days ; i++) {
            String dateVal = "";
            if(i < 10){
                if(month < 10){
                    dateVal = year + "0" + month + "0" + i;
                }else{
                    dateVal = year + "" + month + "0" + i;
                }
            }else{
                if(month < 10){
                    dateVal = year + "0" + month + "" + i;
                }else{
                    dateVal = year + "" + month + "" + i;
                }
            }
            bi.setDateVal(dateVal);
            HumanResourcesBI obj = humanResourcesBIMapper.getMonthWorkPlan(bi);

            sumNumber[i-1] = obj.getSumNumber();
            successNumber[i-1] = obj.getSuccessNumber();
            unSuccessNumber[i-1] = obj.getUnSuccessNumber();
        }
        Object[] dataVal = new Object[3];
        dataVal[0] =  sumNumber;
        dataVal[1] =  successNumber;
        dataVal[2] =  unSuccessNumber;
        cbv.setDataVal(dataVal);
        return cbv;
    }

    /**
     * @Author Shaw
     * @Description 按当月查询日计划完成情况(表格)
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<HumanResourcesBI> getMonthWorkPlanTable() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        HumanResourcesBI bi = new HumanResourcesBI();
        String dateVal = "";
        if(month < 10){
            dateVal = year + "0" + month;
        }else{
            dateVal = year + "" + month;
        }
        bi.setDateVal(dateVal);
        return humanResourcesBIMapper.getMonthWorkPlanTable(bi);
    }

    /**
     * @Author Shaw
     * @Description 按当月查询日客诉处置情况(日走势图)
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public CldBiDataVo getMonthComplaints() {
        CldBiDataVo cbv = new CldBiDataVo();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        Integer[] sumNumber = new Integer[days];
        Integer[] unHandleNumber = new Integer[days];
        Integer[] handleNumber = new Integer[days];
        Integer[] handleOnNumber = new Integer[days];
        HumanResourcesBI bi = new HumanResourcesBI();
        for (int i=1 ; i<=days ; i++) {
            String dateVal = "";
            if(i < 10){
                if(month < 10){
                    dateVal = year + "0" + month + "0" + i;
                }else{
                    dateVal = year + "" + month + "0" + i;
                }
            }else{
                if(month < 10){
                    dateVal = year + "0" + month + "" + i;
                }else{
                    dateVal = year + "" + month + "" + i;
                }
            }
            bi.setDateVal(dateVal);
            HumanResourcesBI obj = humanResourcesBIMapper.getMonthComplaints(bi);

            sumNumber[i-1] = obj.getSumNumber();
            unHandleNumber[i-1] = obj.getUnHandleNumber();
            handleNumber[i-1] = obj.getHandleNumber();
            handleOnNumber[i-1] = obj.getHandleOnNumber();
        }
        Object[] dataVal = new Object[4];
        dataVal[0] =  sumNumber;
        dataVal[1] =  unHandleNumber;
        dataVal[2] =  handleNumber;
        dataVal[3] =  handleOnNumber;
        cbv.setDataVal(dataVal);
        return cbv;
    }

    /**
     * @Author Shaw
     * @Description 按当月查询日客诉处置情况(表格)
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<HumanResourcesBI> getMonthComplaintsTable() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        HumanResourcesBI bi = new HumanResourcesBI();
        String dateVal = "";
        if(month < 10){
            dateVal = year + "0" + month;
        }else{
            dateVal = year + "" + month;
        }
        bi.setDateVal(dateVal);
        return humanResourcesBIMapper.getMonthComplaintsTable(bi);
    }

    /**
     * @Author Shaw
     * @Description 按培训类型查询当月培训完成占比
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String, Object>  getMonthTrainTypeRate(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        HumanResourcesBI bi = new HumanResourcesBI();
        String dateVal = "";
        if(month < 10){
            dateVal = year + "0" + month;
        }else{
            dateVal = year + "" + month;
        }
        bi.setDateVal(dateVal);
        return humanResourcesBIMapper.getMonthTrainTypeRate(bi);
    }

    /**
     * @Author Shaw
     * @Description 按当月查询项目培训计划情况(表格)
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<HumanResourcesBI> getMonthTrainPlanTable() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        HumanResourcesBI bi = new HumanResourcesBI();
        String dateVal = "";
        if(month < 10){
            dateVal = year + "0" + month;
        }else{
            dateVal = year + "" + month;
        }
        bi.setDateVal(dateVal);
        return humanResourcesBIMapper.getMonthTrainPlanTable(bi);
    }

    /**
     * @Author Shaw
     * @Description 按保险在保期中查询保单情况占比
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String, Object>  getStaffInsuranceRate(){
        return humanResourcesBIMapper.getStaffInsuranceRate();
    }

    /**
     * @Author Shaw
     * @Description 按当月查询项目培训计划情况(表格)
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<HumanResourcesBI> getStaffInsuranceTable() {
        return humanResourcesBIMapper.getStaffInsuranceTable(new HumanResourcesBI());
    }
}
